package com.esunny.springboot.controller;


import com.esunny.springboot.constant.CacheConstants;
import com.esunny.springboot.pojo.domain.R;
import com.esunny.springboot.pojo.dto.Permissions;
import com.esunny.springboot.pojo.dto.Principal;
import com.esunny.springboot.service.UserService;
import com.esunny.springboot.utils.GlobalReuseMethod;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 表名全称:t_user  用户表。 前端控制器
 * </p>
 *
 * @author 罗滔
 * @since 2020-10-26
 */
@Api(value = "用户的操作", tags = {"用户相关的操作"})
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @ApiOperation(value = "添加用户", notes = "添加用户")
    @PostMapping("/addUser")
    public R<?> addUser(@RequestParam("userName") String userName,
                        @RequestParam("password") String password,
                        @RequestParam("role") long role,//说明不能为null
                        @ApiParam(name = "permissions", value = "给新建的用户分配的权限", required = true)
                        @RequestBody Permissions permissions) throws Exception {

        Principal principal = (Principal) GlobalReuseMethod.getAttribute(CacheConstants.PRINCIPAL);
        userService.addUser(userName, password, permissions, principal.getUser().getUserId(), role);
        return R.ok(null, "用户添加成功");
    }

    @ApiOperation(value = "删除用户", notes = "删除用户")
    @PostMapping("/deleteUser")
    public R<?> deleteUser(@RequestParam("userName") String userName) throws Exception {

        userService.deleteUser(userName);
        return R.ok(null, "用户删除成功");
    }

    @ApiOperation(value = "修改密码", notes = "修改密码")
    @PostMapping("/updatePassword")
    public R<?> updatePassword(@RequestParam("userName") String userName,
                               @RequestParam("password") String password,
                               @RequestParam("newPassword") String newPassword) throws Exception {

        userService.updatePassword(userName, password, newPassword);
        return R.ok(null, "修改密码成功");
    }

    @ApiOperation(value = "父级修改子集角色", notes = "父级修改子集角色")
    @PostMapping("/updateRole")
    public R<?> updateRole(@RequestParam("userName") String userName,
                           @RequestParam("role") String role) throws Exception {
        //TODO
        userService.updateRole(userName, role);
        return R.ok(null, "修改子集角色成功");
    }

    @ApiOperation(value = "父级给子级修改权限", notes = "父级给子级修改权限")
    @PostMapping("/updatePermissions")
    public R<?> updatePermissions(@RequestParam("userName") String userName,
                                  @ApiParam(name = "permissions", value = "给子级分配的权限", required = true)
                                  @RequestBody Permissions permissions) throws Exception {

        //TODO
        userService.updateChildPermissions(userName, permissions);
        return R.ok(null, "给子级修改权限成功");
    }

    @ApiOperation(value = "超级管理员的权限", notes = "超级管理员的权限")
    @PostMapping("/getSuperAdminPermissions")
    public R<?> getSuperAdminPermissions() throws Exception {

        //TODO
        userService.getSuperAdminPermissions();
        return R.ok(null, "给子级修改权限成功");
    }

    @ApiOperation(value = "查询某个员工的权限", notes = "查询某个员工的权限")
    @PostMapping("/getUserPermissions")
    public R<?> getUserPermissions(@RequestParam("userName") String userName) throws Exception {

        return R.ok(userService.getUserPermissions(userName));
    }
}

